﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaDeBastanteodePoderes.Negocio;
using SistemaDeBastanteodePoderes.Dominio;

namespace SistemaDeBastanteodePoderes.Controllers
{
    public class ClienteController : Controller
    {
        //Dependencias
        public SupervisorServiceImpl SupervisorService { get; set;}
        //
        // GET: /Cliente/

        public ActionResult Index()
        {
            TempData["tipos"] = ListarTipos();
            ICollection<Cliente> modelo = SupervisorService.ListarClientes();
            return View(modelo);
        }

        //
        // GET: /Cliente/Details/5

        public ActionResult Details(int Codigo)
        {
            Cliente modelo = SupervisorService.ObtenerCliente(Codigo);
            return View(modelo);
        }

        //
        // GET: /Cliente/Create

        public ActionResult Create()
        {
            TempData["tipos"] = ListarTipos();
            return View();
        } 

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    Cliente clienteACrear = new Cliente();
                    
                    string nroRUC = (String)collection["nroRUC"];
                    string razSocial = (String)collection["razSocial"];
                    DateTime fecIniOperaciones = DateTime.Parse(collection["fecIniOperaciones"]);
                    int tipEmpresaCodigo = int.Parse(collection["TipoEmp"]);
                    int cntEmpleados = int.Parse(collection["cntEmpleados"]);
                    TempData["tipos"] = ListarTipos();
                    SupervisorService.RegistrarCliente(nroRUC, razSocial, fecIniOperaciones, tipEmpresaCodigo, cntEmpleados);

                }

                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TempData["tipos"] = ListarTipos();
                return View("Create");
            }
        }
        
        //
        // GET: /Cliente/Edit/5

        public ActionResult Edit(int Codigo)
        {
            TempData["tipos"] = ListarTipos();
            return View(SupervisorService.ObtenerCliente(Codigo));
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                TempData["tipos"] = ListarTipos();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Delete/5

        public ActionResult Delete(int Codigo)
        {
            return View(SupervisorService.ObtenerCliente(Codigo));
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private ICollection<SelectListItem> ListarTipos()
        {
            ICollection<SelectListItem> tipos = new List<SelectListItem>();
            tipos.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (TipoEmpresa tipo in SupervisorService.ListarTiposEmpresa())
                tipos.Add(new SelectListItem()
                {
                    Value = tipo.Codigo.ToString(),
                    Text = tipo.Abreviacion
                });
            return tipos;
        }
    }
}
